<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" :width="record.func === '1' ? '620px' : '500px'" top="5vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :model="record" label-width="100px" :rules="rules" label-position="left" :key="key" style="margin:0 30px;">
        <el-form-item :label="$t('协议名称')" class="mmsForm" prop="proto">
          <span style="color:#999;">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="mmsForm" prop="action">
          <el-select class="mmsLength" v-model="record.action" ref="action" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('功能码')" class="mmsForm" prop="func">
          <el-select class="mmsLength" v-model="record.func" ref="func" size='small' filterable>
            <el-option v-for="(value, key) in funcList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('读写属性')" class="mmsForm" prop="read_write_type" v-if="record.func === '1'">
          <el-select class="mmsLength" v-model="record.read_write_type" ref="writeTypes" size='small' filterable>
            <el-option v-for="(value, key) in readWriteTypeList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('读写属性')" class="mmsForm" prop="variable_operate" v-if="record.func === '2'">
          <el-select class="mmsLength" v-model="record.variable_operate" ref='operate' size='small' filterable>
            <el-option v-for="(value, key) in readWriteType2List" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('读写属性')" class="mmsForm" prop="file_operate" v-if="record.func === '3'">
          <el-select class="mmsLength" v-model="record.file_operate" ref="fileOperate" size='small' filterable>
            <el-option v-for="(value, key) in readWriteType3List" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('读写属性')" class="mmsForm" prop="log_operate" v-if="record.func === '4'">
          <el-select class="mmsLength" v-model="record.log_operate" ref="logOperate" size='small' filterable>
            <el-option v-for="(value, key) in readWriteType4List" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('匹配范围')" class="mmsForm" prop="accept_range" v-if="record.func === '1'">
          <el-radio-group v-model="record.accept_range">
            <el-radio label="all" class="all">{{$t('所有')}}</el-radio>
            <el-radio label="single" class="single">{{$t('单个')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-if="record.accept_range === 'single' && record.func === '1'">
          <el-form-item :label="$t('域名')" class="mmsForm" prop="domain_name" key="domain_name">
            <el-input v-model="record.domain_name" ref="name" :maxlength="127" class="mmsLength" size='small' style="margin-right:10px"></el-input>
            eg: BCUGEF650
          </el-form-item>
          <el-form-item :label="$t('变量名')" class="mmsForm" prop="variable_name" key="variable_name">
            <el-input v-model="record.variable_name" ref="variable" :maxlength="127" class="mmsLength" size='small' style="margin-right:10px"></el-input>
            eg:CILO1$CF$Mod$ct
          </el-form-item>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>

  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'MmsDialog',
  data () {
    // var validateAddr = (rule, value, callback) => {
    //   if (String(value).trim() === '') {
    //     callback()
    //   } else if (isNaN(Number(value))) {
    //     callback(new Error(this.$t('只能输入数字')))
    //   } else if (!this.isInteger(value)) {
    //     callback(new Error(this.$t('只能输入整数')))
    //   } else if (Number(value) < 0 || Number(value) > 65535) {
    //     callback(new Error(this.$t('输入范围0~65535')))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      dialogVisible: false,
      key: '1',
      record: {
        accept_range: 'all',
        action: 'accept',
        func: '1',
        match_direction: '-',
        proto: 'MMS',
        read_write_type: 'read',
        variable_operate: '1',
        file_operate: '1',
        log_operate: '1',
        rule_id: null,
        domain_name: '',
        variable_name: ''
      },
      recordCopy: {
        accept_range: 'all',
        action: 'accept',
        func: '1',
        match_direction: '-',
        proto: 'MMS',
        read_write_type: 'read',
        variable_operate: '1',
        file_operate: '1',
        log_operate: '1',
        rule_id: null,
        domain_name: '',
        variable_name: ''
      },
      rule: ruleArray.rule,
      funcList: ruleArray.funcMmsList,
      readWriteTypeList: ruleArray.readWriteTypeList,
      readWriteType2List: ruleArray.readWriteType2List,
      readWriteType3List: ruleArray.readWriteType3List,
      readWriteType4List: ruleArray.readWriteType4List,
      actionList: { 'accept': '允许', 'alert': '告警' },
      rules: {
      }
    }
  },
  computed: {
    change () {
      const { func, accept_range } = this.record
      return { func, accept_range }
    }
  },
  watch: {
    change: {
      handler (newValue) {
        this.key = this.key + 1
        if (newValue.func === '1' && newValue.accept_range === 'single') {
          this.$nextTick(() => {
            const selectData = ['action', 'func', 'writeTypes']
            selectClass(this.$refs, selectData)
            const data = ['name', 'variable']
            inputClass(this.$refs, data)
          })
        } else if (newValue.func === '2') {
          this.$nextTick(() => {
            const selectData = ['action', 'func', 'operate']
            selectClass(this.$refs, selectData)
          })
        } else if (newValue.func === '3') {
          this.$nextTick(() => {
            const selectData = ['action', 'func', 'fileOperate']
            selectClass(this.$refs, selectData)
          })
        } else if (newValue.func === '4') {
          this.$nextTick(() => {
            const selectData = ['action', 'func', 'logOperate']
            selectClass(this.$refs, selectData)
          })
        } else {
          this.$nextTick(() => {
            const selectData = ['action', 'func']
            selectClass(this.$refs, selectData)
          })
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['action', 'func', 'writeTypes']
        selectClass(this.$refs, selectData)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          if (formData.func !== '1') {
            formData.domain_name = ''
            formData.variable_name = ''
          }
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style>
.mmsLength {
  width: 250px;
}
</style>
